#define _CRT_SECURE_NO_WARNINGS 1

#include"string.h"
#include<iostream>

namespace word
{
	string& string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			_capacity = n;
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			_str = tmp;
		}
		return (*this);
	}

	size_t string::find(const char* s, size_t pos) const
	{
		char* tmp = strstr(_str,s);
		if (tmp == nullptr)
		{
			return npos;
		}
		else 
		{
			return tmp - _str;
		}
	}

	char* string::c_str()
	{
		return _str;
	}

	size_t string::find(const char ch, size_t pos) const
	{
		//char c = '\0';
		for (size_t i = 0; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}

	string string::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (pos + len > _size)
		{
			len = _size - pos;
		}
		
		string tmp;
		tmp.reserve(len);
		for (int i = 0; i < len; i++)
		{
			tmp._str[tmp._size++] = _str[pos + i];
		}
		tmp._str[tmp._size] = '\0';
		return tmp;
	}

	ostream& operator << (ostream& out, const string& s)
	{
		//out << s.c_str();
		//string::iterator it = s.begin();

		for (auto ch : s)
		{
			out << ch;
		}

		return out;
	}
//	ostream& operator<<(ostream& out, const string& s)
//	{
////		for (auto ch : s)
////		{
////			out << ch;
////		}
//		//s.find();
//
//		return out;
//	}


	//std::istream& operator >> (std::istream& in, string& s)
	//{

	//}
	
	void test_string()
	{
		string s1("abcdefg");
		size_t i = s1.find('c', 0);
		string s2 = s1.substr(i,20);
		std::cout << s1.c_str() << std::endl;
		std::cout << s2.c_str() << std::endl;
		std::cout << s2 << std::endl;
	}

	void test_string01()
	{
		string s("adafafggas");
		for (auto e : s)
		{
			std::cout << e << "    ";
		}
	}
}